#!/usr/bin/env bash
# SPDX-License-Identifier: MulanPSL-2.0+
# Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved.

mkdir -p /etc/compass-ci
cp -a $CCI_SRC/sparrow/setup.yaml /etc/compass-ci/setup.yaml

server_ip=$(awk '/^server_ip:\s/ {print $2; exit}' /etc/compass-ci/setup.yaml)
[ -z $server_ip ] && server_ip=$(ip route get 1.2.3.4 | awk '{print $7; exit}')

server_name=$(hostname | cut -f1 -d.)
: ${sched_host:=$server_ip}
: ${sched_port:=3000}
: ${ES_HOST:=$server_ip}
: ${ES_PORT:=9200}
: ${OS_HTTP_HOST:=$server_ip}
: ${SEND_MAIL_HOST:=$server_ip}
: ${SEND_MAIL_PORT:=49000}
: ${SRV_HTTP_HOST:=$server_ip}
: ${SRV_HTTP_PORT:=11300}
: ${LOGGING_ES_HOST:=$server_ip}
: ${LOGGING_ES_PORT:=9202}
: ${INITRD_HTTP_HOST:=$server_ip}
: ${INITRD_HTTP_PORT:=8800}
: ${ASSIST_RESULT_HOST:=$server_ip}
: ${ASSIST_RESULT_PORT:=8102}
: ${RESULT_WEBDAV_HOST:=$server_ip}
: ${RESULT_WEBDAV_PORT:=3080}
: ${MASTER_FLUENTD_HOST:=$server_ip}
: ${MASTER_FLUENTD_PORT:=24224}
: ${DOCKER_REGISTRY_HOST:=$server_ip}
: ${DOCKER_REGISTRY_PORT:=5001}
: ${LOCAL_SEND_MAIL_PORT:=11311}
: ${LOCAL_ROBOT_EMAIL_ADDRESS:=Crystal TEAM}

mkdir -p /etc/compass-ci/defaults
cat > /etc/compass-ci/defaults/$server_name.yaml <<EOF
lab: nolab
EOF

mkdir -p /etc/compass-ci/service
cat > /etc/compass-ci/service/service-env.yaml <<EOF
ES_HOST: $ES_HOST
ES_PORT: $ES_PORT
SCHED_HOST: $sched_host
SCHED_PORT: $sched_port
LKP_SERVER: $server_ip
GIT_SERVER: $GIT_SERVER
OS_HTTP_HOST: $OS_HTTP_HOST
GITCACHE_HOST: $gitcache_host
GITCACHE_PORT: $gitcache_port
SRV_HTTP_HOST: $SRV_HTTP_HOST
SRV_HTTP_PORT: $SRV_HTTP_PORT
TASKQUEUE_HOST: $sched_host
SEND_MAIL_HOST: $SEND_MAIL_HOST
SEND_MAIL_PORT: $SEND_MAIL_PORT
LOGGING_ES_HOST: $LOGGING_ES_HOST
LOGGING_ES_PORT: $LOGGING_ES_PORT
INITRD_HTTP_HOST: $INITRD_HTTP_HOST
INITRD_HTTP_PORT: $INITRD_HTTP_PORT
ASSIST_RESULT_HOST: $ASSIST_RESULT_HOST
ASSIST_RESULT_PORT: $ASSIST_RESULT_PORT
RESULT_WEBDAV_HOST: $RESULT_WEBDAV_HOST
RESULT_WEBDAV_PORT: $RESULT_WEBDAV_PORT
MASTER_FLUENTD_HOST: $MASTER_FLUENTD_HOST
MASTER_FLUENTD_PORT: $MASTER_FLUENTD_PORT
DOCKER_REGISTRY_HOST: $DOCKER_REGISTRY_HOST
DOCKER_REGISTRY_PORT: $DOCKER_REGISTRY_PORT
LOCAL_SEND_MAIL_PORT: $LOCAL_SEND_MAIL_PORT
MAILDIR: /srv/cci/Maildir/.compass-ci
LOCAL_ROBOT_EMAIL_ADDRESS: $LOCAL_ROBOT_EMAIL_ADDRESS
EOF

mkdir -p /etc/compass-ci/scheduler
cat > /etc/compass-ci/scheduler/local-testbox-env.yaml <<-EOF
LKP_SERVER:
GIT_SERVER:
GITCACHE_HOST:
GITCACHE_PORT:
SRV_HTTP_HOST:
SRV_HTTP_PORT:
SEND_MAIL_HOST:
SEND_MAIL_PORT:
INITRD_HTTP_HOST:
INITRD_HTTP_PORT:
ASSIST_RESULT_HOST:
ASSIST_RESULT_PORT:
RESULT_WEBDAV_HOST:
RESULT_WEBDAV_PORT:
EOF

cat > /etc/profile.d/compass.sh <<'EOF'
export LKP_SRC=/c/lkp-tests
export CCI_SRC=/c/compass-ci
export REPO_SRC=/c/upstream-repos
export CCI_REPOS=/c

export PATH="$PATH:$CCI_SRC/sbin:$LKP_SRC/sbin:$LKP_SRC/bin"
EOF

mkdir -p /etc/compass-ci/register
cat > /etc/compass-ci/register/register.yaml <<'EOF'
delimiter:
  my_email: delimiter@localhost
  my_name: delimiter
auto-submit:
  my_email: auto-submit@localhost
  my_name: auto-submit
EOF

source /etc/os-release

path=$(dirname ${BASH_SOURCE[0]})/os/${ID}
[ -x "$path" ] || exit 0
. "$path"
